到了第10天了,來做個小練習
這裡我是先做了一個有一些簡單小功能的小程式
這個程式有幾個要求
接著來說一下程式碼xml
的部分就直接說做了什麼變動不一個一個放進來了
(最下面有放上完整的程式碼)
xml
的方面分別做了以下變動
兩個TextViewgravity
改成centertext
改成帳號or密碼顯示位子textSize
改成30dp
兩個ImageViewbackground
改成黑色(方便分辨)scaleType
改成center
Buttontext
改成送出textSize
改成20dp
EditTexttext
改成hint
內容也改成account
接著再到MainActivity
裡設定變數名稱
private Button sendBtn;
private TextView accountTv,passwordTv;
private EditText accountEt,passwordEt;
將各個變數與對應的元件id
綁定
sendBtn = findViewById(R.id.main_send_btn);
accountTv = findViewById(R.id.main_account_tv);
passwordTv = findViewById(R.id.main_password_tv);
accountEt = findViewById(R.id.main_account_et);
passwordEt = findViewById(R.id.main_password_et);
設定點擊事件
這邊是讓它在被點擊後
會宣告兩個String
分別代表account和password裡的內容
並將兩個EditText
裡輸入的內容轉換成字串丟給兩個String
最後依照String
裡內容來將兩個String
分別丟入個別的TextView並顯示
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String accountStr = accountEt.getText().toString();
String passwordStr = passwordEt.getText().toString();
accountTv.setText("帳號:"+accountStr);
passwordTv.setText("密碼:"+passwordStr);
}
});
設定長按事件
當使用者長時間壓著這個Button的時候,它會禁用掉這個按鈕
sendBtn.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
sendBtn.setEnabled(false);
return true;
}
});
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/main_account_tv"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="帳號顯示位子"
android:textSize="30dp"
app:layout_constraintBottom_toTopOf="@+id/guideline16"
app:layout_constraintEnd_toStartOf="@+id/guideline4"
app:layout_constraintHorizontal_bias="0.533"
app:layout_constraintStart_toStartOf="@+id/guideline3"
app:layout_constraintTop_toTopOf="@+id/guideline5" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.12" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.88807786" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.13" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.34" />
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/black"
android:scaleType="center"
app:layout_constraintBottom_toTopOf="@+id/guideline7"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/guideline3"
app:layout_constraintTop_toTopOf="@+id/guideline11"
app:srcCompat="@android:drawable/ic_dialog_email" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/black"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline3"
app:layout_constraintTop_toTopOf="@+id/guideline12"
app:srcCompat="@android:drawable/ic_lock_idle_lock" />
<Button
android:id="@+id/main_send_btn"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="送出"
android:textSize="20dp"
app:layout_constraintBottom_toTopOf="@+id/guideline8"
app:layout_constraintEnd_toStartOf="@+id/guideline15"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/guideline10"
app:layout_constraintTop_toTopOf="@+id/guideline9"
app:layout_constraintVertical_bias="0.0" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.63" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.88" />
<EditText
android:id="@+id/main_account_et"
android:layout_width="0dp"
android:layout_height="0dp"
android:ems="10"
android:hint="account"
android:inputType="text"
app:layout_constraintBottom_toTopOf="@+id/guideline7"
app:layout_constraintEnd_toStartOf="@+id/guideline4"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="@+id/guideline11" />
<EditText
android:id="@+id/main_password_et"
android:layout_width="0dp"
android:layout_height="0dp"
android:ems="10"
android:hint="password"
android:inputType="textPassword"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintEnd_toStartOf="@+id/guideline4"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="@+id/guideline12" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.22" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.46" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.56" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5307798" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.76" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.29683697" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.7" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.24" />
<TextView
android:id="@+id/main_password_tv"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="密碼顯示位子"
android:gravity="center"
android:textSize="30dp"
app:layout_constraintBottom_toTopOf="@+id/guideline6"
app:layout_constraintEnd_toStartOf="@+id/guideline4"
app:layout_constraintStart_toStartOf="@+id/guideline3"
app:layout_constraintTop_toTopOf="@+id/guideline16" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.test;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
private Button sendBtn;
private TextView accountTv,passwordTv;
private EditText accountEt,passwordEt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
sendBtn = findViewById(R.id.main_send_btn);
accountTv = findViewById(R.id.main_account_tv);
passwordTv = findViewById(R.id.main_password_tv);
accountEt = findViewById(R.id.main_account_et);
passwordEt = findViewById(R.id.main_password_et);
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String accountStr = accountEt.getText().toString();
String passwordStr = passwordEt.getText().toString();
accountTv.setText("帳號:"+accountStr);
passwordTv.setText("密碼:"+passwordStr);
}
});
sendBtn.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
sendBtn.setEnabled(false);
return true;
}
});
}
}
簡單的練習就到這裡
下篇會介紹Linear Layout